<!--
    -  Copyright (c) 2018-2025, haohanwork.com All rights reserved.
  -->
<template>
    <div class="execution">
        <basic-container>
            <avue-crud ref="crud"
                       :page.sync="page"
                       :data="tableData"
                       :table-loading="tableLoading"
                       :option="tableOption"
                       @on-load="getList"
                       @search-change="searchChange"
                       @refresh-change="refreshChange"
            >
                <template slot-scope="scope" slot="menu">
                    <el-button type="text"
                               v-show="scope.row.status==='0'"
                               @click="review(scope.row)"
                               size="small">审核
                    </el-button>
                    <el-button type="text"
                               v-show="scope.row.reviewStatus==='3'"
                               @click="beginSettlement(scope.row)"
                               size="small">去结算
                    </el-button>
                </template>
                <template slot="menuLeft">
                    <el-button type="primary" icon="el-icon-edit"  size="small" @click="dialogVisible = true">批量创建账单</el-button>
                </template>
            </avue-crud>
            <el-dialog title="批量生成订单应收账单" :visible.sync="dialogVisible" width="50%">
                <el-row :gutter="20">
                    <el-col :span="4">
                        送货时间:
                    </el-col>
                    <el-col :span="10">
                        <el-date-picker
                                v-model="form.deliveryDate"
                                value-format="yyyy-MM-dd"
                                type="date"
                                placeholder="选择日期">
                        </el-date-picker>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="4">
                        送货批次:
                    </el-col>
                    <el-col :span="10">
                        <el-select v-model="form.buySeq" clearable placeholder="请选择">
                            <el-option
                                    v-for="item in $dic.buySeqOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-col>
                </el-row>
                <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="batchCreate">确 定</el-button>
            </span>
            </el-dialog>
            <el-dialog title="账单审核" :visible.sync="reviewDialogVisible" width="50%">
                <el-row :gutter="20">
                    <el-col :span="4">
                        原账单金额:
                    </el-col>
                    <el-col :span="10">
                        {{reviewForm.originAmount}}
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="4">
                        审核状态:
                    </el-col>
                    <el-col :span="10">
                        <el-select v-model="reviewForm.reviewStatus" clearable placeholder="请选择">
                            <el-option value="3" label="通过审核"></el-option>
                            <el-option value="2" label="不通过审核"></el-option>
                        </el-select>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="4">
                        审核金额:
                    </el-col>
                    <el-col :span="10">
                        <el-input v-model="reviewForm.buyerPayment" clearable placeholder="请输入"></el-input>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="4">
                        审核备注:
                    </el-col>
                    <el-col :span="10">
                        <el-input v-model="reviewForm.remarks"
                                  type="textarea"
                                  :rows="2"
                                  clearable
                                  placeholder="请输入"></el-input>
                    </el-col>
                </el-row>
                <span slot="footer" class="dialog-footer">
            <el-button @click="reviewDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="reviewBill">确 定</el-button>
            </span>
            </el-dialog>
        </basic-container>
    </div>
</template>
<!-- 应收账单 -->
<script>
    import {
        fetchList,
        readySettlement,
        checkReceivable,
        reviewPayment,
        createReceivableBatch
    } from '@/api/scm/finance/collectaccounts'
    import {
        tableOption
    } from '@/const/crud/scm/finance/collectaccounts'
    import {getStore, setStore} from "@/util/store";
    export default {
        name: 'order',
        data() {
            return {
                tableData: [],
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20 // 每页显示多少条
                },
                tableLoading: false,
                tableOption: tableOption,
                pmId: '',
                dialogVisible : false,
                form:{
                    deliveryDate: '',
                    buySeq: ''
                },
                reviewDialogVisible: false,
                reviewForm: {
                    buyerPaymentId: '',
                    originAmount: '',
                    buyerPayment: '',
                    reviewStatus: '3',
                    remarks: ''
                }
            }
        },
        created() {
            this.pmId = getStore({name: "pmId"});
        },
        computed: {
        },
        methods: {
            getList(page, params={}) {
                this.tableLoading = true
                params.pmId = this.pmId;
                params.current = page.currentPage;
                params.size = page.pageSize;
                fetchList(params).then(response => {
                    this.tableData = response.data.data.records
                    this.page.total = response.data.data.total
                    this.tableLoading = false
                })
            },
            /**
             * 搜索回调
             */
            searchChange(form, done) {
                this.getList(this.page, form);
                done();
            },
            /**
             * 刷新回调
             */
            refreshChange(info) {
    this.getList(this.page, this.filterForm(info.searchForm));
},
            /**
             * 批量生成应收账单
             */
            batchCreate(){
                let params = this.form;
                if(!params.buySeq || !params.deliveryDate){
                    this.$message.warning("请选择时间/批次");
                    return;
                }
                params.pmId = this.pmId;
                createReceivableBatch(params).then(res => {
                    if(res.data.code ===0){
                        this.$message.success("批量生成成功,"+res.data.data);
                        this.dialogVisible = false;
                    }else{
                        this.$message.warning("批量生成失败,"+res.data.msg);
                    }
                });
            },
            // 查询是否可审核
            review(row){
                let params = {
                    pmId : this.pmId,
                    buyerPaymentId : row.buyerPaymentId
                };
                checkReceivable(params).then(res =>{
                    if(res.data.code === 0 ){
                        row = res.data.data;
                        this.reviewForm.buyerPaymentId = row.buyerPaymentId;
                        this.reviewForm.originAmount = row.buyerPayment;
                        this.reviewForm.buyerPayment = row.buyerPayment;
                        this.$message.success("可审核");
                        this.reviewDialogVisible = true;
                    }else{
                        this.$message.warning(res.data.msg);
                    }
                });
            },
            // 审核账单
            reviewBill(){
                let params =  {
                    pmId : this.pmId,
                    buyerPaymentId : this.reviewForm.buyerPaymentId,
                    buyerPayment : this.reviewForm.buyerPayment,
                    reviewStatus : this.reviewForm.reviewStatus,
                    remarks : this.reviewForm.remarks,
                };
                reviewPayment(params).then(res => {
                    if(res.data.code === 0 ){
                        if(res.data.data === true){
                            this.$message.success("审核成功");
                            this.reviewDialogVisible = false;
                            this.refreshChange({});
                        }else{
                            this.$message.warning("审核失败");
                        }
                    }else{
                        this.$message.warning(res.data.msg);
                    }
                });
            },
            /**
             * 去结算
             */
            beginSettlement(row) {
                // 应收账单
                let params = {
                    pmId : this.pmId,
                    paymentSn : row.buyerPaymentId,
                    settlementType: '1'
                };
                readySettlement(params).then(res =>{
                    // 返回结算单  跳转结算页
                    if(res.data.code === 0 ){
                        this.$message.success("可结算");
                        this.$router.push({path: '/settleaccounts/settlement', query: res.data.data});
                    }else{
                        this.$message.warning(res.data.msg);
                    }
                });
            }
        }
    }
</script>

<style lang="scss" scoped>
</style>
